package com.atguigu.eduservice.controller;


import com.atguigu.commonutils.JwtUtils;
import com.atguigu.commonutils.R;
import com.atguigu.eduservice.client.UcenterClient;
import com.atguigu.eduservice.entity.EduComment;
import com.atguigu.eduservice.entity.EduCourse;
import com.atguigu.eduservice.service.EduCommentService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 评论 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2021-05-12
 */
@RestController
@RequestMapping("/eduservice/edu-comment")
//@CrossOrigin //用nginx需要加
public class EduCommentController {
    @Autowired
    private EduCommentService commentService;

    @Autowired
    private UcenterClient ucenterClient;

    @ApiOperation(value = "根据课程id查询评论列表")
    @GetMapping("/commentList/{page}/{limit}")
    public R getCommentList(@PathVariable long page,@PathVariable long limit,String courseId){
        Page<EduComment> pageParam = new Page<>(page,limit);
        QueryWrapper<EduComment> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id",courseId);
        commentService.page(pageParam, wrapper);
        List<EduComment> commentList = pageParam.getRecords();

        Map<String, Object> map = new HashMap<>();
        map.put("items", commentList);
        map.put("current", pageParam.getCurrent());
        map.put("pages", pageParam.getPages());
        map.put("size", pageParam.getSize());
        map.put("total", pageParam.getTotal());
        map.put("hasNext", pageParam.hasNext());
        map.put("hasPrevious", pageParam.hasPrevious());
        return R.ok().data(map);
    }

    @ApiOperation(value = "添加评论")
    @PostMapping("saveComment")
    public R save(@RequestBody EduComment comment, HttpServletRequest request) {
        //根据token字符串获取用户id
        String id = JwtUtils.getMemberIdByJwtToken(request);
        if(StringUtils.isEmpty(id)) {
            return R.error().code(28004).message("请登录");
        }
        comment.setMemberId(id);
        //根据用户id查找用户
        R userInfo = ucenterClient.getUserInfoOrder(id);
        System.out.println(userInfo);
        Map<String, Object> data = userInfo.getData();
        System.out.println(data);
        comment.setAvatar((String)data.get("avatar"));
        comment.setNickname((String)data.get("nickname"));
        commentService.save(comment);
        return R.ok();
    }

    @ApiOperation(value = "删除评论")
    @DeleteMapping("deleteComment/{commentId}")
    public R delete(@PathVariable String commentId, HttpServletRequest request){
        //根据token字符串获取用户id
        String uid = JwtUtils.getMemberIdByJwtToken(request);
        boolean result = commentService.deleteById(commentId, uid);
        if(result)
            return R.ok().message("删除成功");
        else{
            return R.error().message("不能删除");
        }
    }

    @ApiOperation(value = "判断是否是该用户的评论")
    @GetMapping("isComment/{commentId}")
    public R isComment(@PathVariable String commentId, HttpServletRequest request){
        //根据token字符串获取用户id
        String uid = JwtUtils.getMemberIdByJwtToken(request);
        boolean isComment = commentService.isComment(commentId, uid);
        return R.ok().data("isComment",isComment);
    }

}

